package com.example.likegifmarker.view.wdigets;

import android.content.Context;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.likegifmarker.R;

import butterknife.BindView;
import butterknife.OnClick;

public class MainTabBar extends BaseView {


    @BindView(R.id.item_index)
    TextView mItemIndex;
    @BindView(R.id.item_city)
    TextView mItemCity;
    @BindView(R.id.item_msg)
    TextView mItemMsg;
    @BindView(R.id.item_my)
    TextView mItemMy;
    @BindView(R.id.item_send)
    ImageView mItemSend;
    @BindView(R.id.cl_root)
    ConstraintLayout mClRoot;
    @BindView(R.id.view_line)
    View mViewLine;

    public MainTabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void initViews(Context context) {
        super.initViews(context);
        mItemIndex.setTag(0);
        mItemCity.setTag(1);
        mItemSend.setTag(2);
        mItemMsg.setTag(3);
        mItemMy.setTag(4);
        setBold(mItemIndex);
        setBold(mItemCity);
        setBold(mItemMsg);
        setBold(mItemMy);
    }

    public void setBg(boolean isWhite, int idx) {
        mClRoot.setBackgroundColor(isWhite ? 0xffffffff : 0xff000000);
        clearSelectedItem();
        mViewLine.setBackgroundColor(isWhite ? 0xfff9f9f9 : 0xff000000);
        getTabItem(idx).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
        getTabItem(idx).setTextColor(isWhite ? 0xff292929 : 0xffffffff);
    }

    private void setBold(TextView textView) {
        TextPaint tp = textView.getPaint();
        tp.setFakeBoldText(true);
    }

    @Override
    public int getLayoutId() {
        return R.layout.tabbar_main;
    }

    @OnClick({R.id.item_index, R.id.item_msg, R.id.item_city, R.id.item_my})
    public void OnClick(TextView item) {
        if (onTabSelectedListener == null) throw new NullPointerException("listener == null");
        int index = (int) item.getTag();
        tab(index != 0, index);
    }

    public void tab(boolean isWhite, int idx) {
        if (onTabSelectedListener.onSelected(idx)) {
            clearSelectedItem();
            getTabItem(idx).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
            mViewLine.setBackgroundColor(isWhite ? 0xfff9f9f9 : 0xff000000);
            getTabItem(idx).setTextColor(isWhite ? 0xff292929 : 0xffffffff);
            if (isWhite) {
                mItemSend.setImageResource(R.mipmap.index_tab_black);
            } else {
                mItemSend.setImageResource(R.mipmap.index_tab_white);
            }
        }
    }

    private TextView getTabItem(int idx) {
        switch (idx) {
            case 0:
                return mItemIndex;
            case 1:
                return mItemCity;
            case 3:
                return mItemMsg;
            case 4:
                return mItemMy;
        }
        return mItemIndex;
    }

    public void clearSelectedItem() {
        mItemIndex.setTextColor(0xff9f9f9f);
        mItemIndex.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
        mItemCity.setTextColor(0xff9f9f9f);
        mItemCity.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
        mItemMsg.setTextColor(0xff9f9f9f);
        mItemMsg.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
        mItemMy.setTextColor(0xff9f9f9f);
        mItemMy.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    }


    private OnTabSelectedListener onTabSelectedListener;

    public void setOnTabSelectedListener(OnTabSelectedListener onTabSelectedListener) {
        this.onTabSelectedListener = onTabSelectedListener;
    }

    public interface OnTabSelectedListener {
        boolean onSelected(int idx);
    }
}
